package com.san.oms.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.san.common.utils.Constant;
import com.san.common.utils.QuotaRuleComparator;
import com.san.oms.dao.ScoreManager;
import com.san.oms.domain.ModelScDTO;
import com.san.oms.domain.QuotaDO;
import com.san.oms.domain.QuotaRuleDTO;
import com.san.oms.domain.ScoreDTO;
import com.san.oms.factory.ScoreManagerFactory;
import com.san.oms.service.QuotaService;
import com.san.oms.service.ScoreService;

/**
 * 评分服务实现
 * 
 * @author Administrator
 *
 */
@Service
public class ScoreServiceImpl implements ScoreService {

	@Autowired
	private QuotaService quotaService;
	
	@Autowired
	private ScoreManagerFactory factory;

	@Override
	public void calc(ModelScDTO modelDTO) throws Exception {

		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("parentId", "0");
		condition.put("modelId", modelDTO.getModelId());
		List<QuotaDO> quotaList = quotaService.findQuotaList(condition);
		ScoreManager scoreManager = factory.getManager();
		scoreManager.bakTable(modelDTO);
		for (QuotaDO qutaDO : quotaList) {
			iteratorQuota(scoreManager, qutaDO, modelDTO);
		}
	}

	/**
	 * 迭代指标模型
	 * 
	 * @param qutaDO
	 * @throws Exception
	 */
	public void iteratorQuota(ScoreManager scoreManager, QuotaDO quotaDO, ModelScDTO modelDTO) throws Exception {
		// 获取一级模型指标
		List<QuotaDO> childQuotaList = quotaService.findSubset(quotaDO.getModelId(), quotaDO.getQuotaId());
		if (null != childQuotaList && childQuotaList.size() > 0) {
			for (QuotaDO subsetDO : childQuotaList) {
				iteratorQuota(scoreManager,subsetDO, modelDTO);
			}

			// 保存父级指标分数
			scoreManager.saveParentQuotaScore(modelDTO, quotaDO, childQuotaList);

		} else {
			this.doCalc(scoreManager,quotaDO, modelDTO);
		}
	}

	public void doCalc(ScoreManager scoreManager, QuotaDO quotaDO, ModelScDTO modelDTO) throws Exception {

		if (null == quotaDO.getQuotaType()) {
			return;
		}

		// 加权
		if (Constant.QUOTA_TYPE_WEIGHT == quotaDO.getQuotaType().intValue()) {
			scoreManager.weightCacl(modelDTO, quotaDO);
		} else if (Constant.QUOTA_TYPE_INTERVAL == quotaDO.getQuotaType().intValue()) {
			scoreManager.intervalCacl(modelDTO, quotaDO);
		}
	}

	/**
	 * 区间规则转换
	 * 
	 * @param quotaRule
	 * @return
	 * @throws Exception
	 */
	public List<QuotaRuleDTO> resolveInterval(String quotaRule) throws Exception {
		if (null == quotaRule || "".equals(quotaRule)) {
			return null;
		}
		String[] rules = quotaRule.split("\\|");
		List<QuotaRuleDTO> list = new ArrayList<QuotaRuleDTO>();

		QuotaRuleDTO dto = null;
		for (String rule : rules) {
			String[] section = rule.split(",");
			dto = new QuotaRuleDTO();

			dto.setNum(section[0].equals("~") ? 999999999 : Integer.parseInt(section[0]));
			dto.setScore(Integer.parseInt(section[1]));
			list.add(dto);
		}

		Collections.sort(list, new QuotaRuleComparator());
		return list;

	}

	@Override
	public List<ScoreDTO> queryScoreDetail(Long modelId, String name, String code) throws Exception {
		ScoreManager scoreManager = factory.getManager();
		
		return scoreManager.queryScoreDetail(modelId, name, code);
	}
	
	@Override
	public ScoreDTO queryScore(Long modelId, String name, String code) throws Exception {
		ScoreManager scoreManager = factory.getManager();
		
		return scoreManager.queryScore(modelId, name, code);
	}
}
